package lucene.server.common.utils;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

public class SystemInfoUtil {
    public static String getCpuInfo() {
        // 获取操作系统MXBean
        OperatingSystemMXBean osMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        if (osMXBean instanceof com.sun.management.OperatingSystemMXBean) {
            return String.format("%.2f%%", ((com.sun.management.OperatingSystemMXBean) osMXBean).getProcessCpuLoad() * 100);
        }else{
            return String.format("%.2f%%", osMXBean.getSystemLoadAverage() * 100);
        }

    }
    public static String getMemoryInfo() {
        // 获取内存MXBean
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        return String.format("%.2f MB", (memoryMXBean.getHeapMemoryUsage().getUsed()+memoryMXBean.getNonHeapMemoryUsage().getUsed()) / (1024.0 * 1024));
    }

    public static  Long calculateFiles(String filePath) {

        try {
             return Files.list(Paths.get(filePath)).count();
        } catch (IOException e) {
            e.printStackTrace();
             return 0L;
        }
    }
}
